
import matplotlib.pyplot as plt
import numpy as np

# 中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
def prove_root_existence():
    """证明方程 2^x + x = 0 在(-1,0)内有唯一实根"""
    x = np.linspace(-1, 0, 1000)
    f = lambda x: 2**x + x
    
    plt.figure(figsize=(10, 6))
    plt.plot(x, f(x), 'b-', linewidth=2, label='f(x) = 2^x + x')
    plt.axhline(y=0, color='k', linestyle='-', alpha=0.3)
    
    # 验证端点异号
    f_minus1, f_0 = f(-1), f(0)
    plt.scatter([-1, 0], [f_minus1, f_0], color='red', s=100)
    plt.annotate(f'f(-1) = {f_minus1:.3f} < 0', (-1, f_minus1), 
                xytext=(-0.85, f_minus1 + 0.1))
    plt.annotate(f'f(0) = {f_0:.3f} > 0', (0, f_0), 
                xytext=(-0.1, f_0 - 0.2))
    
    # 寻找零点
    root_x = x[np.argmin(np.abs(f(x)))]
    plt.scatter([root_x], [f(root_x)], color='green', s=100,
               label=f'零点 ξ ≈ {root_x:.3f}')
    
    plt.title('零点存在定理：方程 2^x + x = 0 在 (-1,0) 内有根')
    plt.xlabel('x'); 
    plt.ylabel('f(x)')
    plt.legend(); 
    plt.grid(True, alpha=0.3)
    plt.show()

prove_root_existence()